#pragma once
#include<iostream>
#include<string>
#include<vector>
#include<assert.h>
using namespace std;

namespace zhao
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;
		vector()
		{ }

		vector()

		~vector()
		{
			assert(_start);
			delete[] _start;
			_start = _finish = _end_of_storage = nullptr;
		}

		iterator begin()
		{
			return _start;
		}
		const_iterator begin() const
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}
		const_iterator end() const
		{
			return _finish;
		}

		size_t size() const
		{
			return _finish - _start;
		}

		size_t capacity() const
		{
			return _end_of_storage - _start;
		}

		bool empty() const
		{
			return _finish == _start;
		}


		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t old_size = size();
				iterator tmp = new T[n];
				memcpy(tmp, _start,size()*sizeof(T));
				delete[] _start;
				_start = tmp;
				_finish = tmp + old_size;
				_end_of_storage= tmp+n;

			}
		}
		void push_back(const T& val)
		{
			if (_finish == _end_of_storage)
			{
				reserve(capacity() == 0 ? 4 : 2 * capacity());
			}
			*_finish = val;
			++_finish;
		}

		iterator insert(iterator pos, const T& x)
		{
			assert(pos >= _start);
			assert(pos <= _finish);
			if (capacity() == size())
			{
				size_t len = pos - _start;
				reserve(capacity()==0?4:2 * capacity());
				pos = _start + len;
			}
			T* end = _finish;
			while (end >= pos)
			{
				*end = *(end - 1);
				--end;
			}
			*pos = x;
			_finish++;

			return pos;
		}

		T& operator[] (size_t n)
		{
			assert(n >= 0);
			return *(_start + n);
		}

		void resize(size_t n, T val = T())
		{
			if (n < size())
			{
				_finish = _start + n;
			}
			else
			{
				reserve(n);
				while (_finish<_start+n)
				{
					*_finish = val;
					++_finish;
				}
			}
		}

		void erase(iterator pos)
		{
			assert(pos >= _start);
			assert(pos <= _finish);
			while (pos+1 <= _finish)
			{
				*pos = *(pos + 1);
				pos++;
			}
			--_finish;
		}

	
	private:
		iterator _start=nullptr;
		iterator _finish=nullptr;
		iterator _end_of_storage=nullptr;
	};
	template <class T>
	void print_vector(const vector<T>& v)
	{
		auto it = v.begin();
		while (it != v.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;

		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;
	}


}